package com.mengxiangnongfu.controller.system;

import com.UPHelper;
import com.jcraft.jsch.JSchException;
import com.mengxiangnongfu.aspectj.annotation.Log;
import com.mengxiangnongfu.aspectj.enums.BusinessType;
import com.mengxiangnongfu.config.utils.ShiroSecurityUtils;
import com.mengxiangnongfu.domain.DictType;
import com.mengxiangnongfu.dto.DicTypeDto;
import com.mengxiangnongfu.service.DictTypeService;
import com.mengxiangnongfu.vo.AjaxResult;
import com.mengxiangnongfu.vo.DataGridView;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import java.io.Serializable;

/**
 * 字典控制器
 *
 * @author Yang
 */
@RestController
@RequestMapping("system/dic/type")
@Slf4j
@Api(value = "字典类型")
public class DictTypeController {

    @Resource
    private DictTypeService dictTypeService;

    /**
     * 查询全部字典信息
     *
     * @return
     */
    @GetMapping("loadAllDic")
    public AjaxResult loadAllDic() {
        DataGridView dataGridView = dictTypeService.loadAllDic();
        return AjaxResult.success("查询全部字典信息成功!", dataGridView.getDataList(), dataGridView.getTotalCount());
    }

    /**
     * 分页查询字典信息
     *
     * @param dicTypeDto
     * @return
     */
    @GetMapping("loadDicByPage")
    public AjaxResult getDicByPage(DicTypeDto dicTypeDto) {
        DataGridView dataGridView = dictTypeService.loadDicByPage(dicTypeDto);
        return AjaxResult.success("分页查询字典信息成功!", dataGridView.getDataList(), dataGridView.getTotalCount());
    }

    /**
     * 添加字典目录
     *
     * @param dicTypeDto
     * @return
     */
    @Log(title = "插入字典", businessType = BusinessType.INSERT)
    @PostMapping("insertDic")
    public AjaxResult insertDic(@Validated DicTypeDto dicTypeDto) {
        if (dictTypeService.dicExist(dicTypeDto.getDictId(), dicTypeDto.getDictType())) {
            return AjaxResult.fail("新增字典[" + dicTypeDto.getDictName() + "]失败,字典类型已经存在!");
        }
        dicTypeDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(this.dictTypeService.insertDic(dicTypeDto));
    }

    /**
     * 判断字典是否存在
     *
     * @param id
     * @param dicType
     * @return
     */
    @GetMapping("dicExist")
    public AjaxResult dicExist(Long id, String dicType) {
        Boolean flag = dictTypeService.dicExist(id, dicType);
        return AjaxResult.success("", flag);
    }

    /**
     * 修改字典目录信息
     *
     * @param dicTypeDto
     * @return
     */
    @Log(title = "修改字典", businessType = BusinessType.UPDATE)
    @PutMapping("modifyDic")
    public AjaxResult modifyDic(@Validated DicTypeDto dicTypeDto) {
        if (dictTypeService.dicExist(dicTypeDto.getDictId(), dicTypeDto.getDictType())) {
            return AjaxResult.fail("修改字典[" + dicTypeDto.getDictName() + "]失败,字典类型已经存在!");
        }
        dicTypeDto.setUpdateBy(ShiroSecurityUtils.getCurrentSimpleUser().getUserName());
        dicTypeDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(this.dictTypeService.modifyDic(dicTypeDto));
    }

    /**
     * 删除字典
     *
     * @param ids
     * @return
     */
    @Log(title = "删除字典", businessType = BusinessType.DELETE)
    @DeleteMapping("removeDic/{ids}")
    public AjaxResult removeDic(@PathVariable @Validated @NotBlank(message = "删除的字典ID不能为空!") Long[] ids) {
        Integer result = dictTypeService.removeDic(ids);
        return result.intValue() > 0 ? AjaxResult.success("删除字典成功,共删除" + ids.length + "条数据!") : AjaxResult.error("删除字典目录失败!");
    }

    /**
     * 根据编号查询字典信息
     *
     * @param id
     * @return
     */
    @GetMapping("getDictTypeById/{id}")
    public AjaxResult getDictTypeById(@PathVariable @Validated @NotEmpty(message = "字典的ID不能为空!") Serializable id) {
        DictType dictType = dictTypeService.getDictTypeById(id);
        return AjaxResult.success("查询成功", dictType);
    }

    /**
     * 同步字典缓存
     *
     * @return
     */
    @Log(title = "同步字典缓存", businessType = BusinessType.OTHER)
    @GetMapping("dictCacheAsync")
    public AjaxResult dictCacheAsync() {
        try {
            this.dictTypeService.dictCacheAsync();
            return AjaxResult.success();
        } catch (Exception ex) {
            log.error(ex.getMessage());
            return AjaxResult.error();
        }
    }
}
